package jltx.fragmentpageframework.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import cn.jltx.utils.XqLog;
import jltx.fragmentpageframework.R;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/22.
 */

public class BottomBar extends LinearLayout{
    private LayoutParams mTabParams;
    private int mCurrentPosition = 0;

    private int selectMskColor = -1;
    private int unSelectMskColor = -1;
    private boolean showBottomText;

    private OnTabSelectedListener onTabSelectedListener;

    public BottomBar(Context context) {
        super(context);
        initView(context,null);
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context,AttributeSet attrs){
        setOrientation(HORIZONTAL);
        mTabParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        mTabParams.weight = 1;


        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorView);
        int n = a.getIndexCount();

        for (int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.ChangeColorView_unSelectMaskColor:
                    unSelectMskColor = a.getColor(attr,unSelectMskColor);
                    break;
                case R.styleable.ChangeColorView_selectMaskColor:
                    selectMskColor = a.getColor(attr,selectMskColor);
                    break;
                case R.styleable.ChangeColorView_showBottomText:
                    showBottomText = a.getBoolean(attr,false);
                    break;

            }
        }
        a.recycle();

    }

    public BottomBar addItem(final BottomBarTab tab) {
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTabSelectedListener == null) return;
                int pos = tab.getTabPosition();
                if (mCurrentPosition == pos) {
                    onTabSelectedListener.onTabReselected(pos);
                } else {
                    tab.setSelected(true);
                    onTabSelectedListener.onTabUnselected(mCurrentPosition);
                    getChildAt(mCurrentPosition).setSelected(false);
                    onTabSelectedListener.onTabSelected(pos, mCurrentPosition);
                    mCurrentPosition = pos;
                }
            }
        });
        tab.setTabPosition(this.getChildCount());
        tab.setLayoutParams(mTabParams);
        tab.setShowBottomText(showBottomText);
        if(unSelectMskColor != -1 && selectMskColor != -1){
            tab.setIconMaskAndTextColor(unSelectMskColor,selectMskColor);
        }
        addView(tab);
        if(mCurrentPosition == tab.getTabPosition()){
            tab.setSelected(true);
        }else {
            tab.setSelected(false);
        }
        return this;
    }

    /**
     * 保存索引
     * @return
     */
    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable supterState = super.onSaveInstanceState();
        return new SaveState(supterState,mCurrentPosition);
    }

    /**
     * 恢复索引
     * @param state
     */
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SaveState ss = (SaveState)state;
        super.onRestoreInstanceState(ss.getSuperState());
        if(mCurrentPosition != ss.position){
            if(mCurrentPosition >=0 && mCurrentPosition < getChildCount()){
                getChildAt(mCurrentPosition).setSelected(false);
            }

            if(ss.position >=0 && ss.position < getChildCount()){
                getChildAt(ss.position).setSelected(true);
            }

        }
        mCurrentPosition = ss.position;
    }

    /**
     * 获取 Tab
     */
    public BottomBarTab getItem(int index) {
        if(index <0 || index > getChildCount())return null;
        return (BottomBarTab)getChildAt(index);
    }

    public boolean isShowBottomText() {
        return showBottomText;
    }

    public void setShowBottomText(boolean showBottomText) {
        this.showBottomText = showBottomText;
        for (int i = 0; i < this.getChildCount(); i++) {
            ((BottomBarTab) getChildAt(i)).setShowBottomText(showBottomText);
        }
    }


    public void onTabPageScrolled(int curPos, int nextPos, float positionOffset){
        if(positionOffset > 0){
            BottomBarTab curTab = (BottomBarTab)getChildAt(curPos);
            BottomBarTab nextTab = (BottomBarTab)getChildAt(nextPos);
            curTab.changeBottomBarTabColor(1 - positionOffset);
            nextTab.changeBottomBarTabColor(positionOffset);
        }
    }
    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        this.onTabSelectedListener = onTabSelectedListener;
    }

    public void setCurrentPosition(int position){
        if(this.mCurrentPosition != position){
            this.mCurrentPosition = position;
            resetOtherTabs();
            getChildAt(mCurrentPosition).setSelected(true);
            XqLog.d("YYYY","setCurrentPosition.true:"+position);
        }
    }

    /**
     * 重置其他的TabIndicator的颜色
     */
    private void resetOtherTabs()
    {
        for (int i = 0; i < this.getChildCount(); i++)
        {
            if(mCurrentPosition != i){
                getChildAt(i).setSelected(false);
                XqLog.d("YYYY","setCurrentPosition.false:"+i);
            }
        }
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int position, int prePosition);
        void onTabUnselected(int position);
        void onTabReselected(int position);
    }

    static class SaveState extends BaseSavedState{
        private int position;

        public SaveState(Parcel source) {
            super(source);
            this.position = source.readInt();
        }

        public SaveState(Parcelable superState, int position) {
            super(superState);
            this.position = position;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
        }

        public static final Creator<SaveState> CREATOR = new Creator<SaveState>() {
            @Override
            public SaveState createFromParcel(Parcel source) {
                return new SaveState(source);
            }

            @Override
            public SaveState[] newArray(int size) {
                return new SaveState[size];
            }
        };

    }
}
